<?php
    /**
     * 点赞（使用redis实现）
     */
class Zan
{
	private $_redis = null; //存储redis实例对象
	private $_pdo = null; //存储pdo实例对象
	private $_num = 1; //默认每次点赞+1
	
	
	/**
	 * 构造方法初始化系统
	 */
	public function __construct()
	{
		// 初始化数据库
		$this->_createRedis();
		// 初始化redis
		$this->_createRedis();
	}
	
	/**
	 * 创建redis实例对象
	 */  
	private function _createRedis()
	{
		$this->_redis = new Redis();
		$this->_redis->connect('localhost',6379);
		$this->_redis->select(0);
	}
	
	/**
	 * 创建PDO实例
	 */
	private function _createPdo()
	{
		try {
			$this->_pdo = new PDO("mysql:host=localhost;dbname=phpok","root","root");
		} catch (PDOexception $e) {
			die($e->getMessage());
		}
	}
	
	public function randInsertZan()
	{
		$sql = "SELECT `id` FORM `saas_story`";
		$stmt = $this->_pdo->prepare($sql);
		$stmt->execute();
		$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
		if (empty($res)) {
			die('暂无数据');
		}
	}
	
	
	
	public function clickZan($user_id,$post_id)
	{
		/**
		 * redis 需要的不是一个
		 * 1.一个有序集合 comment:like 用来存储各个文章的点赞数据 
		 * zadd comment:like 15 1 99 2 88 3
		 */ 
		 
		// 首先判断该文章是否被赞过
		if ($this->_redis->zscore('comment:like',$post_id)) {
			
		} else { //如果没有被赞过
			
			// 将该新闻的记录存入redis
			$this->_redis->zincrby('comment:like',$this->_num,$post_id);
			
			// 点赞记录
			$this->_redis->hset('comment:record',$user_id.":".$post_id,1);
		}
	}
	
	
	
}

$zan = new Zan();			
